package UI;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;

import DBTasks.Pet;
import DBTasks.PetSearch;

@SuppressWarnings("serial")
public class PetResultsPanel extends JPanel implements ActionListener{

	private JButton returnButton;
	private DefaultTableModel model1, model2;
	private JTable table1, table2;
	private JTableHeader headers1, headers2;
	private MainFrame frame;

	public PetResultsPanel(MainFrame mainFrame, int zip, String type, String breed, String gender, String age) {
	
		this.setSize(725, 600);
		this.frame = mainFrame;
		
		JLabel logo = new JLabel("Atlanta Pet Adoption Consortium");
		logo.setFont(new Font("Tahoma", Font.BOLD, 30));
		
		JLabel petResults = new JLabel("Pet Search Results For");
		petResults.setFont(new Font("Tahoma", Font.PLAIN, 18));
		
		returnButton = new JButton("Return");
		returnButton.addActionListener(this);
		
		String[] columnNames1 = {"ZipCode", "Type", "Breed", "Gender", "Age"};
		Object[][] data1 = {{zip, type, breed, gender, age}};
		model1 = new DefaultTableModel(data1, columnNames1);
		table1 = new JTable(model1);
		table1.setRowHeight(30);
		table1.setGridColor(Color.black);
		headers1 = table1.getTableHeader();
		table1.setBorder(BorderFactory.createLineBorder(Color.black));
		headers1.setBorder(BorderFactory.createLineBorder(Color.black));
		
		String[] columnNames2 = {"Shelter Name", "Address", "Phone", "PetID", "Pet Name"};
		ArrayList<Pet> pets = PetSearch.search(type, breed, zip, gender, age);
		Object[][] data2 = new Object[pets.size()][5];
		
		for(int i = 0; i < data2.length; i++) {
			data2[i][0] = pets.get(i).getShelterName();
			data2[i][1] = pets.get(i).getAddress();
			data2[i][2] = pets.get(i).getPhoneNumber();
			data2[i][3] = pets.get(i).getPetId();
			data2[i][4] = pets.get(i).getName();
		}
		
		model2 = new DefaultTableModel(data2, columnNames2);
		table2 = new JTable(model2);
		table2.setGridColor(Color.black);
		table2.setRowHeight(30);
		headers2 = table2.getTableHeader();
		table2.setBorder(BorderFactory.createLineBorder(Color.black));
		headers2.setBorder(BorderFactory.createLineBorder(Color.black));
		
		table2.getColumnModel().getColumn(0).setPreferredWidth(150);
		table2.getColumnModel().getColumn(1).setPreferredWidth(250);
		table2.getColumnModel().getColumn(2).setPreferredWidth(100);
		table2.getColumnModel().getColumn(4).setPreferredWidth(100);
		
        GroupLayout gl = new GroupLayout(this);
		
		gl.setHorizontalGroup(gl.createParallelGroup(GroupLayout.Alignment.CENTER)
				.addComponent(logo)
				.addComponent(petResults)
				.addComponent(headers1)
				.addComponent(table1)
				.addComponent(headers2)
				.addComponent(table2)
				.addGroup(gl.createSequentialGroup()
						.addGap(600)
						.addComponent(returnButton)));
		
	    gl.setVerticalGroup(gl.createSequentialGroup()
	    		.addGap(15)
			    .addComponent(logo)
			    .addGap(40)
			    .addComponent(petResults)
			    .addGap(10)
			    .addComponent(headers1)
			    .addComponent(table1)
			    .addGap(40)
			    .addComponent(headers2)
			    .addComponent(table2)
			    .addGap(20)
			    .addComponent(returnButton)
			    .addGap(40)
	    );
		
		gl.setAutoCreateContainerGaps(true);
		
		this.setLayout(gl);
		
	}
	
	public void paintComponent(Graphics g) {
        super.paintComponents(g);
        g.setColor(Color.blue);
        g.fillRect(0, 0, this.getWidth(), 70);
    }

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource().equals(returnButton)){
			frame.setPanel(frame.getSearch());
		}
	}
	
}
